package com.djhu.hadoop;
/**
 * Created by zw on 2017/12/15.
 */

import com.djhu.hadoop.fileservice.CacheFileService;
import com.djhu.hadoop.fileservice.FileService;
import com.djhu.hadoop.fileservice.HdfsFileService;
import com.djhu.hadoop.xmlreader.FormatXmlReader;
import com.djhu.hadoop.xmlreader.XmlReader;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URI;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
@Configuration
@EnableConfigurationProperties(FileProperties.class)
@ConditionalOnClass(value = {FileService.class, XmlReader.class})
@ConditionalOnProperty(prefix = "spring.boot.hadoop.bigfilewriter", value="enabled", matchIfMissing =true )
public class FileAutoConfiguration {

    @Autowired
    private FileProperties fileProperties;

    @Bean
    public XmlReader xmlReader(){
        return new FormatXmlReader();
    }

    @Bean
    public FileNameGenerator fileNameGenerator(){
        return new DefaultFileNameGenerator();
    }

    @Bean
    public FileSystem fileSystem() throws IOException {
        org.apache.hadoop.conf.Configuration configuration  = new org.apache.hadoop.conf.Configuration();
        if (fileProperties.getConfigPath() != null){
            for (String path:fileProperties.getConfigPath().split(",")){
                configuration.addResource(new Path(path));
            }
        }
        URI uri = URI.create(fileProperties.getUri());
        FileSystem fileSystem = FileSystem.get(uri,configuration);
        return fileSystem;
    }

    @Bean
    public FileService fileService(XmlReader xmlReader,FileNameGenerator fileNameGenerator,FileSystem fileSystem) throws Exception {
        HdfsFileService hdfsFileService = new HdfsFileService();
        hdfsFileService.setFileSystem(fileSystem);
        CacheFileService cacheFileService = new CacheFileService();
        cacheFileService.setFileService(hdfsFileService);
        cacheFileService.setFileNameGenerator(fileNameGenerator);
        cacheFileService.setXmlReader(xmlReader);
        cacheFileService.setDefaultDir(fileProperties.getDir());
        cacheFileService.setFilename(fileProperties.getFileprefix());
        cacheFileService.setMaxFileSize(fileProperties.getMaxlength());
        cacheFileService.setFileCacheSize(fileProperties.getCachesize());
        cacheFileService.afterPropertiesSet();
        return cacheFileService;
    }

    public static void main(String[] args) throws IOException {
//        org.apache.hadoop.conf.Configuration configuration  = new org.apache.hadoop.conf.Configuration();
//        ///configuration.set("fs.hdfs.impl", DistributedFileSystem.class.getName());
//        configuration.addResource("core-site.xml");
//        configuration.addResource("hdfs-site.xml");
//        FileSystem fileSystem = FileSystem.get(configuration);
//        System.out.println(fileSystem.getStatus(new Path("/user")).getUsed() );

    }

}